//
//  ContactCell.swift
//  AddressBookKit
//
//  Created by tkgz on 16/5/27.
//  Copyright © 2016年 taoJ. All rights reserved.
//

import UIKit

@objc
protocol ContactCellDelegate: NSObjectProtocol {
    optional func contactCellEditorEmail(contactCell: ContactCell, newContact: Contact)
}

private let checkButtonBorderWidth: CGFloat = 1
private let checkButtonCornerRadius: CGFloat = 3

private let kJudyClintonTitle = "Judy Clinton"
private let kCheckBoxEnableColor = UIColor(red: 0.7143, green: 0.7143, blue: 0.7143, alpha: 1.0)

class ContactCell: UITableViewCell {
    
    weak var delegate: ContactCellDelegate?
    var checkBoxBackgroundColor: UIColor = UIColor(red: 0.2561, green: 0.6658, blue: 0.673, alpha: 1.0)
    var contact: Contact? {
        willSet{
            if let contactValue = newValue {
                checkButton.layer.borderColor = checkBoxBackgroundColor.CGColor
                checkButton.backgroundColor = UIColor.whiteColor()
                if contactValue.isSelected {
                    checkButton.backgroundColor = checkBoxBackgroundColor
                }
                
                fullNameLabel.text = contactValue.firstName + " " + contactValue.lastName
                
                let isHaveEmail = contactValue.contactEmails.count > 0
                addEmailButton.hidden = isHaveEmail
                emailLabel.hidden = !isHaveEmail
                if isHaveEmail { emailLabel.text = contactValue.contactEmails.first?.email }
                
                checkButton.enabled = true
                if contactValue.contactEmails.count == 0 {
                    checkButton.enabled = false
                    checkButton.layer.borderColor = kCheckBoxEnableColor.CGColor
                }
            }
        }
    }
    
    @IBOutlet weak var checkButton: UIButton!
    @IBOutlet weak var fullNameLabel: UILabel!
    @IBOutlet weak var emailLabel: UILabel!
    @IBOutlet weak var addEmailButton: UIButton!
    
    private lazy var editorEmailAlertView: AlertView = {
        let editorEmailAlertView = AlertView.build(kJudyClintonTitle, placeholder: "Email Address")
        editorEmailAlertView.delegate = self
        
        return editorEmailAlertView
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        checkButton.layer.borderWidth = checkButtonBorderWidth
        checkButton.layer.cornerRadius = checkButtonCornerRadius
    }
    
    class func cell() -> ContactCell {
        return NSBundle.mainBundle().loadNibNamed(self.className, owner: nil, options: nil).last as! ContactCell
    }
    
    @IBAction func didTapAddEmail(sender: AnyObject) {
        editorEmailAlertView.showInView()
    }
    
    deinit {
        print("ContactCell: deinit")
    }
}

extension ContactCell: AlertViewDelegate {
    func alertViewTapOK(alertView: AlertView, textField: UITextField) {
        let email = textField.text!
        if !email.isValidEmail {
            UIAlertView(title: "message", message: "invalid Email", delegate: nil, cancelButtonTitle: "ok").show()
            return
        }
        
        editorEmailAlertView.hide()
        
        let contactEmail = ContactEmail()
        contactEmail.localizedEmailLabel = ""
        contactEmail.email = email
        
        contact?.contactEmails.append(contactEmail)
        print("contactEmail: \(contactEmail)")
        delegate?.contactCellEditorEmail?(self, newContact: contact!)
    }
}

